package com.ssy.lingxi.contract.serviceimpl;


import com.ssy.lingxi.contract.entity.SignatureAccount;
import com.ssy.lingxi.contract.entity.SignatureLog;
import com.ssy.lingxi.contract.entity.SignatureOrganization;
import com.ssy.lingxi.contract.esign.constant.EsignConstants;
import com.ssy.lingxi.contract.esign.request.*;
import com.ssy.lingxi.contract.model.bo.sign.SignAuthDetailBO;
import com.ssy.lingxi.contract.model.constant.sign.SignTypeEnum;
import com.ssy.lingxi.contract.model.constant.sign.SignatureAuthTypeEnum;

import java.util.Objects;

/**
 * 电子签章服务实现基类
 * @author yzc
 * @version 2.0.0
 * @date 2020/9/1
 */
public class BaseSignatureService {

    /**
     * 获取签署人盖章关键字
     * @param signAuthDetail 电子合同认证详情
     * @return 个人账户信息名称
     */
    public String getKeyword(SignAuthDetailBO signAuthDetail) {
        // 企业认证
        if (SignatureAuthTypeEnum.LEGAL_PERSON.getCode().equals(signAuthDetail.getAuthType())
                || SignatureAuthTypeEnum.AGENT.getCode().equals(signAuthDetail.getAuthType())) {
            // 机构账户信息
            SignatureOrganization organization = signAuthDetail.getOrganization();
            if (Objects.isNull(organization)) {
                return null;
            }

            return organization.getOrgLegalName();
        }

        // 个人认证
        if (SignatureAuthTypeEnum.PERSONAL.getCode().equals(signAuthDetail.getAuthType())) {
            // 个人账户信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return null;
            }

            return account.getName();
        }

        return null;
    }

    /**
     * 填充甲方签署记录
     * @param signAuthDetail 电子合同认证详情
     * @param signatureLog 电子签章记录实体类
     * @return 电子签章记录实体类
     */
    public SignatureLog platformSignatureLog(SignAuthDetailBO signAuthDetail, SignatureLog signatureLog) {
        // 企业认证
        if (SignatureAuthTypeEnum.LEGAL_PERSON.getCode().equals(signAuthDetail.getAuthType())
                || SignatureAuthTypeEnum.AGENT.getCode().equals(signAuthDetail.getAuthType())) {
            // 机构账户信息
            SignatureOrganization organization = signAuthDetail.getOrganization();
            if (Objects.isNull(organization)) {
                return null;
            }

            signatureLog.setPlatformAccountId(organization.getOrgId());
            signatureLog.setPlatformSignType(SignTypeEnum.ORGANIZATION.getCode());

        }

        // 个人认证
        if (SignatureAuthTypeEnum.PERSONAL.getCode().equals(signAuthDetail.getAuthType())) {
            // 个人账户信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return null;
            }

            signatureLog.setPlatformAccountId(account.getAccountId());
            signatureLog.setPlatformSignType(SignTypeEnum.PERSONAL.getCode());
        }

        return signatureLog;
    }

    /**
     * 填充乙方签署记录
     * @param signAuthDetail 电子合同认证详情
     * @param signatureLog 电子签章记录实体类
     * @return 填充乙方签署记录
     */
    public SignatureLog autoSignatureLog(SignAuthDetailBO signAuthDetail, SignatureLog signatureLog) {
        // 企业认证
        if (SignatureAuthTypeEnum.LEGAL_PERSON.getCode().equals(signAuthDetail.getAuthType())
                || SignatureAuthTypeEnum.AGENT.getCode().equals(signAuthDetail.getAuthType())) {
            // 机构账户信息
            SignatureOrganization organization = signAuthDetail.getOrganization();
            if (Objects.isNull(organization)) {
                return signatureLog;
            }

            signatureLog.setSignAccountId(organization.getOrgId());
            signatureLog.setSignType(SignTypeEnum.ORGANIZATION.getCode());

        }

        // 个人认证
        if (SignatureAuthTypeEnum.PERSONAL.getCode().equals(signAuthDetail.getAuthType())) {
            // 个人账户信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return signatureLog;
            }

            signatureLog.setSignAccountId(account.getAccountId());
            signatureLog.setSignType(SignTypeEnum.PERSONAL.getCode());
        }

        return signatureLog;
    }

    /**
     * 填充创建流程请求
     * @param signAuthDetail 电子合同认证详情
     * @return 填充创建流程请求
     */
    public SignFlowCreateRequest getSignFlowCreateRequest(SignAuthDetailBO signAuthDetail) {
        SignFlowCreateRequest signFlowCreateRequest = new SignFlowCreateRequest();
        // 企业认证
        if (SignatureAuthTypeEnum.LEGAL_PERSON.getCode().equals(signAuthDetail.getAuthType())
                || SignatureAuthTypeEnum.AGENT.getCode().equals(signAuthDetail.getAuthType())) {
            // 机构账户信息
            SignatureOrganization organization = signAuthDetail.getOrganization();
            if (Objects.isNull(organization)) {
                return null;
            }

            signFlowCreateRequest.setInitiatorAccountId(organization.getCreator());
            signFlowCreateRequest.setInitiatorAuthorizedAccountId(organization.getOrgId());
        }

        // 个人认证
        if (SignatureAuthTypeEnum.PERSONAL.getCode().equals(signAuthDetail.getAuthType())) {
            // 个人账户信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return null;
            }

            signFlowCreateRequest.setInitiatorAccountId(account.getAccountId());
            signFlowCreateRequest.setInitiatorAuthorizedAccountId(account.getAccountId());
        }

        return signFlowCreateRequest;
    }

    /**
     * 获取签署印章
     * @param signAuthDetail 电子合同认证详情
     * @return 签署印章
     */
    public String getSealId(SignAuthDetailBO signAuthDetail) {
        // 企业认证
        if (SignatureAuthTypeEnum.LEGAL_PERSON.getCode().equals(signAuthDetail.getAuthType())
                || SignatureAuthTypeEnum.AGENT.getCode().equals(signAuthDetail.getAuthType())) {
            // 机构账户信息
            SignatureOrganization organization = signAuthDetail.getOrganization();
            if (Objects.isNull(organization)) {
                return null;
            }

            return organization.getSealId();
        }

        // 个人认证
        if (SignatureAuthTypeEnum.PERSONAL.getCode().equals(signAuthDetail.getAuthType())) {
            // 个人账户信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return null;
            }

            return account.getSealId();
        }

        return null;
    }

    /**
     * 填充签署区域
     * @param signAuthDetail 电子合同认证详情
     * @return 签署区域
     */
    public SignFieldAutoRequest.Signfield getSignField(SignAuthDetailBO signAuthDetail) {
        SignFieldAutoRequest.Signfield signfieldsBean = new SignFieldAutoRequest.Signfield();

        // 企业认证
        if (SignatureAuthTypeEnum.LEGAL_PERSON.getCode().equals(signAuthDetail.getAuthType())
                || SignatureAuthTypeEnum.AGENT.getCode().equals(signAuthDetail.getAuthType())) {
            // 机构账户信息
            SignatureOrganization organization = signAuthDetail.getOrganization();
            if (Objects.isNull(organization)) {
                return signfieldsBean;
            }
            signfieldsBean.setAuthorizedAccountId(organization.getOrgId());

        }

        // 个人认证
        if (SignatureAuthTypeEnum.PERSONAL.getCode().equals(signAuthDetail.getAuthType())) {
            // 个人账户信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return signfieldsBean;
            }

            signfieldsBean.setAuthorizedAccountId(account.getAccountId());
        }

        return signfieldsBean;
    }

    /**
     * 填充手动签署区域
     * @param signAuthDetail 电子合同认证详情
     * @param fileId  电子合同认证详情
     * @return 手动签署区域
     */
    public SignFieldHandSignRequest.Signfield getHandSignField(SignAuthDetailBO signAuthDetail, String fileId) {
        SignFieldHandSignRequest.Signfield signfieldsBean = new SignFieldHandSignRequest.Signfield();

        signfieldsBean.setFileId(fileId);
        signfieldsBean.setSignType(EsignConstants.signType.NO_LIMIT);

        // 企业认证
        if (SignatureAuthTypeEnum.LEGAL_PERSON.getCode().equals(signAuthDetail.getAuthType())
                || SignatureAuthTypeEnum.AGENT.getCode().equals(signAuthDetail.getAuthType())) {
            // 机构账户信息
            SignatureOrganization organization = signAuthDetail.getOrganization();
            if (Objects.isNull(organization)) {
                return signfieldsBean;
            }
            // 关联人信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return signfieldsBean;
            }
            signfieldsBean.setSignerAccountId(account.getAccountId());
            signfieldsBean.setAuthorizedAccountId(organization.getOrgId());

        }

        // 个人认证
        if (SignatureAuthTypeEnum.PERSONAL.getCode().equals(signAuthDetail.getAuthType())) {
            // 个人账户信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return signfieldsBean;
            }

            signfieldsBean.setSignerAccountId(account.getAccountId());
        }

        return signfieldsBean;
    }

    /**
     * 填充签署地址请求
     * @return 签署地址请求
     */
    public ExecuteUrlRequest getExecuteUrl(SignAuthDetailBO signAuthDetail, String flowId) {
        ExecuteUrlRequest.ExecuteUrlRequestBuilder builder = ExecuteUrlRequest.builder()
                .flowId(flowId);

        // 企业认证
        if (SignatureAuthTypeEnum.LEGAL_PERSON.getCode().equals(signAuthDetail.getAuthType())
                || SignatureAuthTypeEnum.AGENT.getCode().equals(signAuthDetail.getAuthType())) {
            // 机构账户信息
            SignatureOrganization organization = signAuthDetail.getOrganization();
            if (Objects.isNull(organization)) {
                return builder.build();
            }
            // 关联人信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return builder.build();
            }

            return builder.accountId(account.getAccountId())
                    .organizeId(organization.getOrgId())
                    .build();
        }

        // 个人认证
        if (SignatureAuthTypeEnum.PERSONAL.getCode().equals(signAuthDetail.getAuthType())) {
            // 个人账户信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return builder.build();
            }

            return builder.accountId(account.getAccountId())
                    .build();
        }

        return builder.build();
    }

    /**
     * 填充流程信息
     * @param signAuthDetail 电子合同认证详情
     * @return 流程信息
     */
    public SignFlowCreateOneStepRequest.FlowInfoBean getFlowInfo(SignAuthDetailBO signAuthDetail) {
        SignFlowCreateOneStepRequest.FlowInfoBean flowInfoBean = new SignFlowCreateOneStepRequest.FlowInfoBean();

        // 企业认证
        if (SignatureAuthTypeEnum.LEGAL_PERSON.getCode().equals(signAuthDetail.getAuthType())
                || SignatureAuthTypeEnum.AGENT.getCode().equals(signAuthDetail.getAuthType())) {
            // 机构账户信息
            SignatureOrganization organization = signAuthDetail.getOrganization();
            if (Objects.isNull(organization)) {
                return flowInfoBean;
            }
            // 关联人信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return flowInfoBean;
            }

            flowInfoBean.setInitiatorAccountId(account.getAccountId());
            flowInfoBean.setInitiatorAuthorizedAccountId(organization.getOrgId());
        }

        // 个人认证
        if (SignatureAuthTypeEnum.PERSONAL.getCode().equals(signAuthDetail.getAuthType())) {
            // 个人账户信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return flowInfoBean;
            }

            flowInfoBean.setInitiatorAccountId(account.getAccountId());
        }

        return flowInfoBean;
    }

    /**
     * 填充一键签署,签署人账号
     * @param signAuthDetail 电子合同认证详情
     * @return 一键签署,签署人账号
     */
    public SignFlowCreateOneStepRequest.Signer.SignerAccount getOneStepSignerAccount(SignAuthDetailBO signAuthDetail) {
        SignFlowCreateOneStepRequest.Signer.SignerAccount signerAccount = new SignFlowCreateOneStepRequest.Signer.SignerAccount();

        // 企业认证
        if (SignatureAuthTypeEnum.LEGAL_PERSON.getCode().equals(signAuthDetail.getAuthType())
                || SignatureAuthTypeEnum.AGENT.getCode().equals(signAuthDetail.getAuthType())) {
            // 机构账户信息
            SignatureOrganization organization = signAuthDetail.getOrganization();
            if (Objects.isNull(organization)) {
                return signerAccount;
            }
            // 关联人信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return signerAccount;
            }

            signerAccount.setSignerAccountId(account.getAccountId());
            signerAccount.setAuthorizedAccountId(organization.getOrgId());
        }

        // 个人认证
        if (SignatureAuthTypeEnum.PERSONAL.getCode().equals(signAuthDetail.getAuthType())) {
            // 个人账户信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return signerAccount;
            }

            signerAccount.setSignerAccountId(account.getAccountId());
        }

        return signerAccount;
    }

    /**
     * 填充一键签署,签署区域
     * @param signAuthDetail 电子合同认证详情
     * @return 一键签署,签署区域
     */
    public SignFlowCreateOneStepRequest.Signer.Signfield getOneStepSignField(SignAuthDetailBO signAuthDetail) {
        SignFlowCreateOneStepRequest.Signer.Signfield signfield = new SignFlowCreateOneStepRequest.Signer.Signfield();

        // 企业认证
        if (SignatureAuthTypeEnum.LEGAL_PERSON.getCode().equals(signAuthDetail.getAuthType())
                || SignatureAuthTypeEnum.AGENT.getCode().equals(signAuthDetail.getAuthType())) {
            // 机构账户信息
            SignatureOrganization organization = signAuthDetail.getOrganization();
            if (Objects.isNull(organization)) {
                return signfield;
            }
            // 关联人信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return signfield;
            }

            signfield.setActorIndentityType(EsignConstants.ACTOR_INDENTITY_TYPE);
        }

        // 个人认证
        if (SignatureAuthTypeEnum.PERSONAL.getCode().equals(signAuthDetail.getAuthType())) {
            // 个人账户信息
            SignatureAccount account = signAuthDetail.getAccount();
            if (Objects.isNull(account)) {
                return signfield;
            }

            // none
        }

        return signfield;
    }
}